package cc.rengu.igas.channel.wechat.biz.impl;

import cc.rengu.igas.channel.wechat.common.constant.WechatServiceConstant;
import cc.rengu.igas.channel.wechat.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.wechat.facade.WechatPayService;
import cc.rengu.igas.channel.wechat.facade.request.*;
import cc.rengu.igas.channel.wechat.facade.response.*;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;


public class WechatPayServiceImpl implements WechatPayService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<WechatMicropayResponse> micropay(WechatMircopayRequest wechatConsumeByCardRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatMicropayResponse> result = new Result<>();
        WechatMicropayResponse micropayResponse = new WechatMicropayResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(wechatConsumeByCardRequest, WechatServiceConstant.MICROPAY_SERVICE, WechatMicropayResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatMicropayResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("被扫支付失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                micropayResponse.setRespDesc(((BizException) e).getCode());
                micropayResponse.setRespDesc(e.getMessage());
            } else {
                micropayResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                micropayResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<WechatUnifiedOrderResponse> unifiedOrder(WechatUnifiedOrderRequest wechatUnifiedOrderRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatUnifiedOrderResponse> result = new Result<>();
        WechatUnifiedOrderResponse unifiedOrderResponse = new WechatUnifiedOrderResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(wechatUnifiedOrderRequest, WechatServiceConstant.UNIFIED_ORDER_SERVICE, WechatUnifiedOrderResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatUnifiedOrderResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("统一下单失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                unifiedOrderResponse.setRespDesc(((BizException) e).getCode());
                unifiedOrderResponse.setRespDesc(e.getMessage());
            } else {
                unifiedOrderResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                unifiedOrderResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<WechatCancelOrderResponse> cancelOrder(WechatCancelOrderRequest wechatCancelOrderRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatCancelOrderResponse> result = new Result<>();
        WechatCancelOrderResponse cancelOrderResponse = new WechatCancelOrderResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(wechatCancelOrderRequest,
                    WechatServiceConstant.CANCEL_ORDER_SERVICE, WechatCancelOrderResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatCancelOrderResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("撤销订单失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                cancelOrderResponse.setRespDesc(((BizException) e).getCode());
                cancelOrderResponse.setRespDesc(e.getMessage());
            } else {
                cancelOrderResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                cancelOrderResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<WechatCloseOrderResponse> closeOrder(WechatCloseOrderRequest wechatCloseOrderRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatCloseOrderResponse> result = new Result<>();
        WechatCloseOrderResponse closeOrderResponse = new WechatCloseOrderResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(wechatCloseOrderRequest,
                    WechatServiceConstant.CLOSE_ORDER_SERVICE, WechatCloseOrderResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatCloseOrderResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("关闭订单失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                closeOrderResponse.setRespDesc(((BizException) e).getCode());
                closeOrderResponse.setRespDesc(e.getMessage());
            } else {
                closeOrderResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                closeOrderResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<WechatQueryOrderResponse> queryOrder(WechatQueryOrderRequest wechatQueryOrderRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatQueryOrderResponse> result = new Result<>();
        WechatQueryOrderResponse queryOrderResponse = new WechatQueryOrderResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(wechatQueryOrderRequest,
                    WechatServiceConstant.QUERY_ORDER_SERVICE, WechatQueryOrderResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatQueryOrderResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("查询订单状态失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                queryOrderResponse.setRespDesc(((BizException) e).getCode());
                queryOrderResponse.setRespDesc(e.getMessage());
            } else {
                queryOrderResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                queryOrderResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<WechatRefundOrderResponse> refundOrder(WechatRefundOrderRequest wechatRefundOrderRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatRefundOrderResponse> result = new Result<>();
        WechatRefundOrderResponse refundOrderResponse = new WechatRefundOrderResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(wechatRefundOrderRequest,
                    WechatServiceConstant.REFUND_SERVICE, WechatRefundOrderResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatRefundOrderResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("退款交易失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                refundOrderResponse.setRespDesc(((BizException) e).getCode());
                refundOrderResponse.setRespDesc(e.getMessage());
            } else {
                refundOrderResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                refundOrderResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<WechatQuerySingleRefundResponse> querySingleRefund(WechatQuerySingleRefundRequest wechatQuerySingleRefundRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatQuerySingleRefundResponse> result = new Result<>();
        WechatQuerySingleRefundResponse querySingleRefundResponse = new WechatQuerySingleRefundResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(wechatQuerySingleRefundRequest,
                    WechatServiceConstant.SINGLE_REFUND_QUERY_SERVICE, WechatQuerySingleRefundResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatQuerySingleRefundResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("单笔退款查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                querySingleRefundResponse.setRespDesc(((BizException) e).getCode());
                querySingleRefundResponse.setRespDesc(e.getMessage());
            } else {
                querySingleRefundResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                querySingleRefundResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }


}
